package com.itunion.ddd.common.swagger;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.security.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * Swagger配置
 *
 * @author liangliang.zheng
 * @date 2022年08月19日14:22:41
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Value("${swagger.enable:false}")
    private boolean enableSwagger;
    @Value("${swagger.title:itunion-ddd-framework}")
    private String titleSwagger;
    @Value("${swagger.description:基于领域驱动设计（DDD）的微服务快速开发框架}")
    private String descriptionSwagger;
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                // 文档开关,测试环境使用,生产环境关闭
                .enable(enableSwagger)
                //将Timestamp类型全部转为Long类型
                .directModelSubstitute(Timestamp.class, Long.class)
                //将Date类型全部转为Long类型
                .directModelSubstitute(Date.class, Long.class)
                .directModelSubstitute(LocalDateTime.class, Long.class)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(titleSwagger)
                .description(descriptionSwagger)
                .termsOfServiceUrl("itunion-ddd-framework")
                .contact(new Contact("IT实战联盟", "https://mp.weixin.qq.com/s/tsqs2QspLCQxZEpAAOWfUg", "zhenghhgz@163.com"))
                .version("v1.0.0")
                .build();
    }
}